package edu.jkd.controller;


import edu.jkd.entity.Product;
import edu.jkd.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author holyfox
 * @since 2022-11-28
 */
@Controller
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping("index")
    public ModelAndView getProduct() {

        ModelAndView mav = new ModelAndView();
        //1调用查询
        List<Product> productList = productService.list();
        //2数据绑定
        mav.addObject("productList", productList);
        //3页面跳转
        mav.setViewName("index");
        return mav;
    }

    @GetMapping("product_page")
    public ModelAndView getAllProduct() {

        ModelAndView mav = new ModelAndView();
        //1调用查询
        List<Product> productList = productService.list();
        //2数据绑定
        mav.addObject("productList", productList);
        //3页面跳转
        mav.setViewName("product_page");
        return mav;
    }


    @GetMapping("product/{id}")
    public ModelAndView getById(@PathVariable("id") Integer id) {

        Product product = productService.getById(id);
        ModelAndView mav = new ModelAndView("detail");
        mav.addObject("product", product);

        return mav;
    }



}